<template>
	<view>
		<view class="product-window"
			:class="(attr.cartAttr === true ? 'on' : '') + ' ' + (iSbnt?'join':'') + ' ' + (iScart?'joinCart':'') + ' ' + (isBargain?'padding-safe':'')">
			
			<view class="textpic acea-row row-between-wrapper">
				<view class="pictrue">
					<image :src="attr.productSelect.image" @click='getpreviewImage'></image>
				</view>
				<view class="text">
					<view class="line1">
						{{ attr.productSelect.store_name }}
					</view>
					
					<view class="money font-color">
						<view class="acea-row row-middle" v-if="isLadder == 1">
							￥<text class="num">{{ ladderPrice }}</text>
						</view>
						<view class="acea-row row-middle" v-else>
							￥<text class="num">{{ attr.productSelect.price }}</text>
							<text class='vip-money'
								v-if="is_vip>0 && attr.productSelect.vip_price">￥{{attr.productSelect.vip_price}}</text>
							<view class="vipImg" v-if="is_vip>0 && attr.productSelect.vip_price">
								<image src="../../static/images/svip.gif"></image>
							</view>
						</view>
						
						<view class="" style="font-size: 28rpx; color: #999;">
							已选：{{ attrValue }}
						</view>
						<!-- <text class="stock" v-if='isShow'>库存: {{ attr.productSelect.stock }}</text> -->
						<!-- <text class='stock' v-if="limitNum">限量: {{attr.productSelect.quota}}</text> -->
					</view>
				</view>
				<view class="iconfont icon-guanbi" @click="closeAttr"></view>
			</view>
			<scroll-view :scroll-y="true" style="max-height: 60vh;">
			<!-- 配送区域 -->
			<view class="address_area" v-if="showAddress">
				<view class="title_hd">配送区域</view>
				<view class="address flex" @tap="onAddress">
					<view class="">
						<text class="iconfont icon-dizhi"></text>
						<!-- {{item.province}}{{item.city}}{{item.district}}{{item.detail}} -->
						<text class="gray">
						{{ addressInfo.province ? addressInfo.province + addressInfo.city +addressInfo.district + addressInfo.detail : '请选择配送区域' }}
						</text>
					</view>
					<text class="iconfont icon-jiantou gray"></text>
				</view>
			</view>
			
			<view class="rollTop">
				<!-- 产品规格 -->
				<view class="productWinList">
					<view class="item" v-for="(item, indexw) in attr.productAttr" :key="indexw">
						<view class="title">{{ item.attr_name }}</view>
						<view class="listn acea-row row-middle">
							<view class="itemn" :class="item.index === itemn.attr ? 'on' : ''"
								v-for="(itemn, indexn) in item.attr_value" @click="tapAttr(indexw, indexn)"
								:key="indexn">
								{{ itemn.attr }}
							</view>
						</view>
					</view>
				</view>
				<!-- 月配商品显示 -->
				<view class="month_goods" style="margin-top: 20rpx;" v-if="goodsType == 2">
					<view class="productWinList">
						<view class="item" v-for="(item, indexw) in attr.monthGoodsAttr" :key="indexw">
							<view class="title">{{ item.name }}</view>
							<view class="listn acea-row row-middle">
								<view 
									class="itemn" 
									:class="itemn.check ? 'on' : ''"
									style="min-width: 250rpx; text-align: center;"
									v-for="(itemn, indexn) in item.attr_value" 
									@click="tapMonthAttr(indexw, indexn)"
									:key="indexn">
									{{ itemn.attr }}
									<text style="display: block;">{{ itemn.desc }}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
				<!-- 数量加减 -->
				<view class="cart acea-row row-between-wrapper row-center">
					<view class="title">数量</view>
					<view class="carnum acea-row row-left">
						<view class="item reduce acea-row row-center-wrapper"
							:class="attr.productSelect.cart_num <= 1 ? 'on' : ''" @click="CartNumDes">
							<text class="iconfont icon-shangpinshuliang-jian"></text>
						</view>
						<view class='item num acea-row row-middle'>
							<input type="number" v-model="attr.productSelect.cart_num"
								data-name="productSelect.cart_num"
								:disabled="limitNum > 0"
								@input="bindCode(attr.productSelect.cart_num)"></input>
						</view>
						<view v-if="iSplus" class="item plus acea-row row-center-wrapper" :class="
				      attr.productSelect.cart_num >= attr.productSelect.stock
				        ? 'on'
				        : ''
				    " @click="CartNumAdd">
							<text class="iconfont icon-shangpinshuliang-jia"></text>
						</view>
						<view v-else class='item plus'
							:class='(attr.productSelect.cart_num >= attr.productSelect.quota) || (attr.productSelect.cart_num >= attr.productSelect.product_stock) || (attr.productSelect.cart_num >= attr.productSelect.num)? "on":""'
							@click='CartNumAdd'>+</view>
					</view>
				</view>
			</view>
			
			<!-- 自选日期商品 -->
			<view class="address_area" style="border-bottom: 0;" v-if="goodsType == 3">
				<view class="address flex">
					<view class="" style="font-size: 30rpx; color: #999;">送达日期</view>
					<view class="acea-row" style="flex: 1;">
						<picker 
							mode="selector" 
							:range="attr.optionTime" 
							:value="0" 
							@change="changeLog" 
							style="width: 100%; text-align: right;">
						    <view class="acea-row row-right">
								{{ datetimesingle ? datetimesingle : '请选择' }}
								<text class="text-red" style="margin-left: 10rpx;">{{week}}</text>
								<text class="iconfont icon-jiantou gray"></text>
							</view>
						</picker>
					</view>
				</view>
			</view>
			<!-- 优惠加购 -->
			<view class="discount" v-if="attr.plusList && attr.plusList.length > 0">
				<view class="title_hd">优惠加购<text style="color: #999;">（ 可多选 ）</text></view>
				<scroll-view :scroll-x="true" class="discount_scroll">
					<view class="item" v-for="(item, index) in attr.plusList" :key="index" @click="discountClick(index)">
						<image :src="item.image" mode=""></image>
						<view class="pro_name line2">{{ item.store_name }}x1</view>
						<view class="price">
							<text style="color: #D9001B;" v-if="item.is_gift == 1">赠送</text>
							<text v-if="item.is_gift == 0">￥{{ item.price }}</text>
							<text 
								class="gray" 
								style="text-decoration: line-through; margin-left: 10rpx;"
							>￥{{ item.ot_price }}</text>
						</view>
						<view class="select-box" :class="item.checked ? 'act_select' : ''"></view>
					</view>
				</scroll-view>
			</view>
			</scroll-view>
			<!-- 砍价商品按钮 -->
			<view class="bargainBnt" v-if="isBargain" @click="confirm">确定支付</view>
			<view class="joinBnt bg-color"
				v-if="iSbnt && attr.productSelect.product_stock>0 &&attr.productSelect.quota>0" @click="goCat">我要参团
			</view>
			<view class="joinBnt on"
				v-else-if="(iSbnt && attr.productSelect.quota<=0)||(iSbnt &&attr.productSelect.product_stock<=0)">已售罄
			</view>
			<view class="joinBnt bg-color" v-if="iScart && attr.productSelect.stock" @click="goCat">确定</view>
			<view class="joinBnt on" v-else-if="iScart && !attr.productSelect.stock">已售罄</view>
		</view>
		<view class="mask" @touchmove.prevent :hidden="attr.cartAttr === false" @click="closeAttr"></view>
	</view>
</template>

<script>
	export default {
		props: {
			attr: {
				type: Object,
				default: () => {}
			},
			attrValue: {
				type: String,
				default: ''
			},
			limitNum: {
				type: Number,
				value: 0
			},
			isShow: {
				type: Number,
				value: 0
			},
			showAddress: {
				type: Boolean,
				default: true
			},
			iSbnt: {
				type: Number,
				value: 0
			},
			iSplus: {
				type: Number,
				value: 0
			},
			iScart: {
				type: Number,
				value: 0
			},
			is_vip: {
				type: Number,
				value: 0
			},
			addressInfo: {
				type: Object,
				default: () => {
					return {}
				}
			},
			goodsType: { // 1：普通商品，2：月配商品，3：自选日期商品
				type: [String, Number],
				default: 1
			},
			isBargain: {
				type: Boolean,
				default: false
			},
			isLadder: {
				type: [String, Number],
				default: 0
			},
			ladderPrice: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				discountList: [],
				selectIndex: [],
				datetimesingle: '',
				week: '',
			}
		},
		mounted() {
			
		},
		methods: {
			changeLog(e) {
				const { value } = e.detail
			    this.datetimesingle = this.attr.optionTime[value].substr(0, 10)
				var weekDay = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
				var myDate = new Date(Date.parse(this.datetimesingle));  
				this.week = weekDay[myDate.getDay()]
				this.$emit('chooseDate', this.datetimesingle)
			},
			getpreviewImage: function() {
				uni.previewImage({
					urls: this.attr.productSelect.image.split(','),
					current: this.attr.productSelect.image
				});
			},
			goCat: function() {
				this.$emit('goCat');
			},
			confirm: function() {
				this.$emit('goPay');
			},
			/**
			 * 购物车手动输入数量
			 * 
			 */
			bindCode: function(e) {
				this.$emit('iptCartNum', this.attr.productSelect.cart_num);
			},
			closeAttr: function() {
				this.$emit('myevent');
			},
			CartNumDes: function() {
				this.$emit('ChangeCartNum', false);
			},
			CartNumAdd: function() {
				this.$emit('ChangeCartNum', true);
			},
			tapAttr: function(indexw, indexn) {
				let that = this;
				that.$emit("attrVal", {
					indexw: indexw,
					indexn: indexn
				});
				this.$set(this.attr.productAttr[indexw], 'index', this.attr.productAttr[indexw].attr_values[indexn]);
				let value = that.getCheckedValue().join(",");
				that.$emit("ChangeAttr", value);
			},
			tapMonthAttr(indexw, indexn) {
				let that = this;
				that.$emit("monthAttrVal", JSON.stringify({
					indexw: indexw,
					indexn: indexn
				}));
				// this.$set(this.attr.monthGoodsAttr[indexw], 'index', this.attr.monthGoodsAttr[indexw].attr_value[indexn]);
				// this.attr.monthGoodsAttr[indexw].attr_value[indexn].check = true
				// let value = that
				// 	.getMonthCheckedValue()
				// 	.join(",");
				// that.$emit("ChangeMonthAttr", value);
			},
			getMonthCheckedValue: function() {
				
				let productAttr = this.attr.monthGoodsAttr;
				let value = [];
				for (let i = 0; i < productAttr.length; i++) {
					for (let j = 0; j < productAttr[i].attr_value.length; j++) {
						if (productAttr[i].index === productAttr[i].attr_value[j]) {
							value.push(productAttr[i].attr_value[j]);
						}
					}
				}
				return value;
			},
			//获取被选中属性；
			getCheckedValue: function() {
				let productAttr = this.attr.productAttr;
				let value = [];
				for (let i = 0; i < productAttr.length; i++) {
					for (let j = 0; j < productAttr[i].attr_values.length; j++) {
						if (productAttr[i].index === productAttr[i].attr_values[j]) {
							value.push(productAttr[i].attr_values[j]);
						}
					}
				}
				return value;
			},
			discountClick(index) {
				this.$emit('plusIds', index)
			},
			// 配送区域点击事件
			onAddress() {
				this.$emit('onAddress')
			},
			// 日期选择
			chooseDate() {
				this.$emit('chooseDate', this.datetimesingle)
			}
		}
	}
</script>

<style scoped lang="scss">
	.vip-money {
		color: #282828;
		font-size: 28rpx;
		font-weight: 700;
		margin-left: 6rpx;
	}

	.vipImg {
		width: 68rpx;
		height: 27rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}
	.address_area, .discount {
		width: 100%;
		padding: 10rpx 30rpx;
		// border-bottom: 1px solid #ddd;
		.title_hd {
			width: 100%;
			padding: 0 0 20rpx;
			color: #999;
			font-size: 30rpx;
		}
		.address {
			justify-content: space-between;
			align-items: center;
			border-bottom: 1px solid #eee;
			padding-bottom: 15rpx;
		}
		.text-red { 
			color: #F62C2C;
		}
		.gray {
			color: #adabab;
		}
		.discount_scroll {
			display: flex;
			white-space: nowrap;
			.item {
				position: relative;
				display: inline-block;
				// width: 30%;
				&:not(:last-child) {
					margin-right: 30rpx;
				}
				image {
					width: 150rpx;
					height: 150rpx;
					margin-bottom: 10rpx;
					border-radius: 15rpx;
				}
				.pro_name {
					width: 150rpx;
					white-space: pre-line;
					font-size: 24rpx;
				}
				.price {
					font-size: 20rpx;
				}
				.select-box {
					position: absolute;
					top: 10rpx;
					right: 10rpx;
					width: 40rpx;
					height: 40rpx;
					border: 1px solid #d5d4d4;
					border-radius: 50%;
					&::before {
						content: '';
						position: absolute;
						top: 50%;
						left: 50%;
						width: 15rpx;
						height: 10rpx;
						transform: translate(-50%, -75%) rotate(-55deg);
						border: 1px solid #d5d4d4;
						border-top-color: transparent;
						border-right-color: transparent;
					}
				}
				.act_select {
					background-color: #ff5637;
					border-color: #ff5637;
					&::before {
						border-bottom-color: #fff;
						border-left-color: #fff;
					}
				}
			}
		}
	}
	 .discount {
		padding: 0 30rpx 30rpx;
		.title_hd {
			padding: 10rpx 0 20rpx 0;
		}
	 }
	.product-window {
		position: fixed;
		bottom: 0;
		width: 100%;
		left: 0;
		background-color: #fff;
		z-index: 100;
		border-radius: 16rpx 16rpx 0 0;
		transform: translate3d(0, 100%, 0);
		transition: all .3s cubic-bezier(.25, .5, .5, .9);
		padding-bottom: 100rpx;
		padding-bottom: calc(100rpx + constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
		padding-bottom: calc(100rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
	}
	.padding-safe {
		padding-bottom: calc(constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
		padding-bottom: calc(env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
	}
	.bargainBnt {
		font-size: 30rpx;
		font-weight: bold;
		color: #fff;
		width: 93%;
		height: 80rpx;
		border-radius: 40rpx;
		background-image: linear-gradient(to right, #f67a38 0%, #f11b09 100%);
		text-align: center;
		line-height: 80rpx;
		margin: 32rpx auto 10rpx;
	}
	.product-window.on {
		transform: translate3d(0, 0, 0);
	}

	.product-window.join {
		padding-bottom: 30rpx;
	}

	.product-window.joinCart {
		padding-bottom: 30rpx;
		z-index: 10000;
	}

	.product-window .textpic {
		padding: 0 130rpx 20rpx 30rpx;
		margin-top: 29rpx;
		position: relative;
	}

	.product-window .textpic .pictrue {
		width: 150rpx;
		height: 150rpx;
	}

	.product-window .textpic .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 10rpx;
	}

	.product-window .textpic .text {
		width: 410rpx;
		font-size: 32rpx;
		color: #202020;
	}

	.product-window .textpic .text .money {
		font-size: 24rpx;
		margin-top: 20rpx;
	}

	.product-window .textpic .text .money .num {
		font-size: 36rpx;
	}

	.product-window .textpic .text .money .stock {
		color: #999;
		margin-left: 6rpx;
	}

	.product-window .textpic .iconfont {
		position: absolute;
		right: 30rpx;
		top: -5rpx;
		font-size: 35rpx;
		color: #8a8a8a;
	}

	.product-window .rollTop {
		// max-height: 500rpx;
		// overflow: auto;
		margin-top: 20rpx;
	}

	.product-window .productWinList .item~.item {
		margin-top: 20rpx;
	}

	.product-window .productWinList .item .title {
		font-size: 30rpx;
		color: #999;
		padding: 0 30rpx;
	}

	.product-window .productWinList .item .listn {
		padding: 0 30rpx 0 16rpx;
	}

	.product-window .productWinList .item .listn .itemn {
		// border: 1px solid #F2F2F2;
		font-size: 26rpx;
		color: #282828;
		padding: 7rpx 33rpx;
		border-radius: 10rpx;
		margin: 20rpx 0 0 14rpx;
		background-color: #F2F2F2;
	}

	.product-window .productWinList .item .listn .itemn.on {
		// color: #E93323;
		background: #ffddd7;
		// border-color: #E93323;
	}

	.product-window .productWinList .item .listn .itemn.limit {
		color: #999;
		text-decoration: line-through;
	}

	.product-window .cart {
		margin: 20rpx 0;
		padding: 0 30rpx;
	}

	.product-window .cart .title {
		font-size: 30rpx;
		color: #999;
	}

	.product-window .cart .carnum {
		height: 54rpx;
		// margin-top: 24rpx;
	}

	.product-window .cart .carnum .iconfont {
		font-size: 25rpx;
	}

	.product-window .cart .carnum view {
		// border: 1px solid #a4a4a4;
		width: 84rpx;
		text-align: center;
		height: 100%;
		line-height: 54rpx;
		color: #282828;
		font-size: 45rpx;
	}

	.product-window .cart .carnum .reduce {
		border-right: 0;
		border-radius: 6rpx 0 0 6rpx;
		line-height: 48rpx;
		font-size: 60rpx;
	}

	.product-window .cart .carnum .reduce.on {
		// border-color: #e3e3e3;
		color: #DEDEDE;
	}

	.product-window .cart .carnum .plus {
		border-left: 0;
		border-radius: 0 6rpx 6rpx 0;
		line-height: 46rpx;
	}

	.product-window .cart .carnum .plus.on {
		// border-color: #e3e3e3;
		color: #dedede;
	}

	.product-window .cart .carnum .num {
		background: rgba(242, 242, 242, 1);
		color: #282828;
		font-size: 28rpx;
	}

	.product-window .joinBnt {
		font-size: 30rpx;
		width: 620rpx;
		height: 86rpx;
		border-radius: 50rpx;
		text-align: center;
		line-height: 86rpx;
		color: #fff;
		margin: 21rpx auto 0 auto;
	}

	.product-window .joinBnt.on {
		background-color: #bbb;
		color: #fff;
	}
</style>
